package com.dimples.dd.system.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dimples.dd.common.pojo.PageResult;
import com.dimples.dd.system.convert.SmsLogConvert;
import com.dimples.dd.system.enums.sms.SmsReceiveStatusEnum;
import com.dimples.dd.system.enums.sms.SmsSendStatusEnum;
import com.dimples.dd.system.mapper.SystemSmsLogMapper;
import com.dimples.dd.system.model.entity.SystemSmsLogDO;
import com.dimples.dd.system.model.entity.SystemSmsTemplateDO;
import com.dimples.dd.system.model.query.SmsLogPageQuery;
import com.dimples.dd.system.model.vo.SmsLogPageVO;
import com.dimples.dd.system.service.SystemSmsLogService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.Objects;

@Service
@RequiredArgsConstructor
public class SystemSmsLogServiceImpl extends ServiceImpl<SystemSmsLogMapper, SystemSmsLogDO> implements SystemSmsLogService {

    private final SmsLogConvert smsLogConvert;

    @Override
    public PageResult<SmsLogPageVO> selectSmsLogPage(SmsLogPageQuery pageQuery) {
        PageResult<SystemSmsLogDO> pageResult = this.baseMapper.selectPage(pageQuery);
        return this.smsLogConvert.entity2Page(pageResult);
    }

    @Override
    public Long createSmsLog(String mobile, Long userId, Integer userType, Boolean isSend, SystemSmsTemplateDO template, String templateContent, Map<String, Object> templateParams) {
        SystemSmsLogDO.SystemSmsLogDOBuilder logBuilder = SystemSmsLogDO.builder();
        // 根据是否要发送，设置状态
        logBuilder.sendStatus(Objects.equals(isSend, true) ? SmsSendStatusEnum.INIT.getStatus() : SmsSendStatusEnum.IGNORE.getStatus());
        // 设置手机相关字段
        logBuilder.mobile(mobile).userId(userId).userType(userType);
        // 设置模板相关字段
        logBuilder.templateId(template.getId()).templateCode(template.getCode()).templateType(template.getType());
        logBuilder.templateContent(templateContent).templateParams(templateParams)
                .apiTemplateId(template.getApiTemplateId());
        // 设置渠道相关字段
        logBuilder.channelId(template.getChannelId()).channelCode(template.getChannelCode());
        // 设置接收相关字段
        logBuilder.receiveStatus(SmsReceiveStatusEnum.INIT.getStatus());

        // 插入数据库
        SystemSmsLogDO logDO = logBuilder.build();
        this.baseMapper.insert(logDO);
        return logDO.getId();
    }

    @Override
    public void updateSmsSendResult(Long id, Boolean success, String apiSendCode, String apiSendMsg, String apiRequestId, String apiSerialNo) {
        SmsSendStatusEnum sendStatus = success ? SmsSendStatusEnum.SUCCESS : SmsSendStatusEnum.FAILURE;
        this.baseMapper.updateById(SystemSmsLogDO.builder().id(id)
                .sendStatus(sendStatus.getStatus()).sendTime(LocalDateTime.now())
                .apiSendCode(apiSendCode).apiSendMsg(apiSendMsg)
                .apiRequestId(apiRequestId).apiSerialNo(apiSerialNo).build());
    }

    @Override
    public void updateSmsReceiveResult(Long id, Boolean success, LocalDateTime receiveTime, String apiReceiveCode, String apiReceiveMsg) {
        SmsReceiveStatusEnum receiveStatus = Objects.equals(success, true) ?
                SmsReceiveStatusEnum.SUCCESS : SmsReceiveStatusEnum.FAILURE;
        this.baseMapper.updateById(SystemSmsLogDO.builder().id(id).receiveStatus(receiveStatus.getStatus())
                .receiveTime(receiveTime).apiReceiveCode(apiReceiveCode).apiReceiveMsg(apiReceiveMsg).build());
    }
}
